package com.hruiworks.campusgroup.controller;

import com.hruiworks.campusgroup.common.BaseResponse;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.manager.entity.ActivityData;
import com.hruiworks.campusgroup.pojo.entity.Activity;
import com.hruiworks.campusgroup.pojo.entity.ActivityGrade;
import com.hruiworks.campusgroup.pojo.entity.User;
import com.hruiworks.campusgroup.pojo.query.ActivityPageQuery;
import com.hruiworks.campusgroup.pojo.request.ActivityCreateRequest;
import com.hruiworks.campusgroup.service.ActivityGradeService;
import com.hruiworks.campusgroup.service.ActivityService;
import com.hruiworks.campusgroup.utils.AuthorityUtils;
import com.hruiworks.campusgroup.utils.ResultUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 活动相关Controller
 *
 * @author jiushui
 */
@RestController
@RequestMapping("/api/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @Resource
    private ActivityGradeService activityGradeService;

    /**
     * 用户申请活动
     *
     * @param activityCreateRequest 活动创建请求体
     * @param httpServletRequest http请求体
     * @return 申请结果
     */
    @PostMapping("/apply")
    public BaseResponse<Boolean> applyForActivity(@RequestBody ActivityCreateRequest activityCreateRequest,
                                                  HttpServletRequest httpServletRequest) {
        Long userId = AuthorityUtils.currentUser(httpServletRequest).getId();
        Boolean result = activityService.applyForActivity(activityCreateRequest, userId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 分页查询活动列表
     * @param activityPageQuery
     * @return
     */
    @PostMapping("/page")
    public BaseResponse<PageVO<Activity>> listPageActivity(@RequestBody ActivityPageQuery activityPageQuery, HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        PageVO<Activity> activityPageVO = activityService.listPageActivity(activityPageQuery, user);
        return ResultUtils.success(activityPageVO, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 获取活动详情
     * @param activityId 活动id
     * @return
     */
    @GetMapping("/detail")
    public BaseResponse<Activity> oneActivity(@RequestParam("activity_id") Long activityId) {
        Activity activity = activityService.oneActivity(activityId);
        return ResultUtils.success(activity, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 分页获取用户参与过活动的部分信息
     * @param size
     * @param current
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/history/data")
    public BaseResponse<PageVO<ActivityData>> listPageActivityData(@RequestParam("size") Long size,
                                                                   @RequestParam("current") Long current,
                                                                   @RequestParam("search_value") String searchValue,
                                                                   HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        PageVO<ActivityData> activityDataPageVO = activityService.selfHistoryActivityData(size, current,searchValue, user);
        return ResultUtils.success(activityDataPageVO, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 提交用户对于某个活动的评分和评价
     * @param activityId
     * @param grade
     * @param comment
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/grade/self/submit")
    public BaseResponse<Boolean> submitGrade(@RequestParam("activity_id") Long activityId,
                                             @RequestParam("grade") Integer grade,
                                             @RequestParam("comment") String comment,
                                             HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Boolean result = activityGradeService.submitGrade(activityId, grade, comment, user);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 获取用户对于活动的自己的评分
     * @param activityId
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/grade/self/get")
    public BaseResponse<ActivityGrade> selfGrade(@RequestParam("activity_id") Long activityId,
                                             HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        ActivityGrade selfGrade = activityGradeService.getSelfGrade(activityId, user);
        return ResultUtils.success(selfGrade, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 更新用户对于某个活动的评分
     * @param activityId
     * @param grade
     * @param comment
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/grade/self/update")
    public BaseResponse<Boolean> updateSelfGrade(@RequestParam("activity_id") Long activityId,
                                                 @RequestParam("grade") Integer grade,
                                                 @RequestParam("comment") String comment,
                                                 HttpServletRequest httpServletRequest) {
        User user = AuthorityUtils.currentUser(httpServletRequest);
        Boolean result = activityGradeService.updateSelfGrade(activityId, grade, comment, user);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 分页获取整个活动的评分
     * @param current
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/grade/all/page")
    public BaseResponse<PageVO<ActivityGrade>> pageAllGrade(@RequestParam("current") Long current,
                                                            @RequestParam("page_size") Long pageSize,
                                                            @RequestParam("activity_id") Long activityId) {
        PageVO<ActivityGrade> activityGradePageVO = activityGradeService.pageActivityGrade(current, pageSize, activityId);
        return ResultUtils.success(activityGradePageVO, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 获取某个活动的评论数量
     * @param activityId 活动id
     * @return
     */
    @GetMapping("/grade/comment/count")
    public BaseResponse<Long> commentCount(@RequestParam("activity_id") Long activityId) {
        Long count = activityGradeService.activityCommentCount(activityId);
        return ResultUtils.success(count, SuccessCode.COMMON_SUCCESS);
    }


}
